<?php
/**
 * 订单查询接口 jd.union.open.order.query
 *
 * User: Ken.Zhang <kenphp@yeah.net>
 * Date: 2019-05-12
 */
class UnionOpenOrderQueryRequest {

    /**
     * 页码，返回第几页结果
     */
    private $pageNo;

    /**
     * 每页包含条数，上限为500
     */
    private $pageSize;

    /**
     * 订单时间查询类型(1：下单时间，2：完成时间，3：更新时间)
     */
    private $type;

    /**
     * 查询时间，建议使用分钟级查询，
     * 格式：yyyyMMddHH、yyyyMMddHHmm或yyyyMMddHHmmss，
     * 如201811031212 的查询范围从12:12:00--12:12:59
     */
    private $time;

    /**
     * 子站长ID（需要联系运营开通PID账户权限才能拿到数据），childUnionId和key不能同时传入
     */
    private $childUnionId;

    /**
     * 其他推客的授权key，查询工具商订单需要填写此项，childUnionid和key不能同时传入
     */
    private $key;

    /**
     * 请求参数
     */
    public $apiParams = [];

    public function setPageNo($page_no)
    {
        $this->pageNo = $page_no;
        $this->apiParams['orderReq']['pageNo'] = $page_no;
    }

    public function getPageNo()
    {
        return $this->pageNo;
    }

    public function setPageSize($page_size)
    {
        $this->pageSize = $page_size;
        $this->apiParams['orderReq']['pageSize'] = $page_size;
    }

    public function getPageSize()
    {
        return $this->pageSize;
    }

    public function setType($type)
    {
        $this->type = $type;
        $this->apiParams['orderReq']['type'] = $type;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setTime($time)
    {
        $this->time = $time;
        $this->apiParams['orderReq']['time'] = $time;
    }

    public function getTime()
    {
        return $this->time;
    }

    public function setChildUnionId($child_union_id)
    {
        $this->childUnionId = $child_union_id;
        $this->apiParams['orderReq']['childUnionId'] = $child_union_id;
    }

    public function getChildUnionId()
    {
        return $this->childUnionId;
    }

    public function setKey($key)
    {
        $this->key = $key;
        $this->apiParams['orderReq']['key'] = $key;
    }

    public function getKey()
    {
        return $this->key;
    }

    public function getApiMethodName()
    {
        return 'jd.union.open.order.query';
    }

    /**
     * 解析结果
     *
     * @param   string      $response
     * @throws  Exception
     * @return  mixed
     */
    public function getResult($response)
    {
        $responseKey = str_replace('.', '_', $this->getApiMethodName()) . '_response';

        $response = @json_decode($response, true);
        if (!array_key_exists($responseKey, $response))
            $resp = $response['errorResponse'];
        else {
            $response[$responseKey]['result'] = json_decode($response[$responseKey]['result']);

            $resp = $response[$responseKey];
        }

        return $resp;
    }

}